<?php

namespace App\Admin\Forms;

use App\Helpers\Lib\WechatMini;
use App\Models\TongDistrict;
use App\Models\TongOrder;
use App\Models\TongUser;
use App\Models\TongUserApplyStore;
use Dcat\Admin\Traits\LazyWidget;
use Dcat\Admin\Widgets\Form;
use Illuminate\Http\Request;
use Illuminate\Support\Facades\DB;

class AuditApplyStoreForm extends Form
{
    use LazyWidget;

    /**
     * Handle the form request.
     *
     * @param array $input
     *
     * @return mixed
     */
    public function handle(array $input)
    {

//        TongOrder::query()->where('id',$input['id'])->update([
//            'express_num'=>$input['express_num'],
//            'express_company'=>$input['express_company'],
//            'send_time'=>date('Y-m-d h:i:s',time()),
//            'status'=>4,//已发货，待收货
//        ]);
        DB::transaction(function () use($input) {
            //修改审核的状态
            TongUserApplyStore::query()->where('id',$input['id'])->update(['status'=>$input['status']]);
            if($input['status']==2){
                $applyStoreInfo =  TongUserApplyStore::query()->where('id',$input['id'])->first();
                //审核通过将用户的身份修改成为认证店铺
                $result = TongUser::query()->where('id',$applyStoreInfo['u_id'])->update([
                    'is_real_store'=>1
                ]);
                if($result){
                    updateUserRealStore($applyStoreInfo['u_id'],1);
                }
            }
        });

        return $this
				->response()
				->success('审核成功')
				->refresh();
    }

    /**
     * Build a form here.
     */
    public function form()
    {
        //$id = $this->payload['id'] ?? null;
        $this->hidden('id','id');

        $this->radio('status','是否通过')
            ->when(3,function(Form $form){
                $this->textarea('status_desc','拒绝的原因')->rules('required_if:status,3',['status.required_if'=>'请输入拒绝的原因'])
                    ->setLabelClass(['asterisk'])
                    ->rows(5);
            })
            ->options(['2' => '通过', '3'=> '拒绝'])
            ->default('2');
    }

    /**
     * The data of the form.
     *
     * @return array
     */
    public function default()
    {
        return [
            'id'=>$this->payload['id'] ?? null,
        ];
    }
}
